package meicai.appium.utils;

import java.time.Duration;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

public class SwipeScreen {
	private AndroidDriver driver;
	private int appWidth;
	private int appHeight;

	/**
	 * 构造方法，对三个变量进行初始化
	 * 
	 * @param driver
	 */
	public SwipeScreen(AndroidDriver driver) {
		this.driver = driver;
		this.appWidth = driver.manage().window().getSize().getWidth();
		this.appHeight = driver.manage().window().getSize().getHeight();
	}

	/**
	 * 向左滑动
	 * 
	 * @param duration
	 *            滑动时间，单位是毫秒
	 */
	public void swipeToLeft(int duration) {
		int startx = appWidth * 9 / 10;
		int endx = appWidth * 1 / 10;
		int y = appHeight * 1 / 2;
		try {
			TouchAction act = new TouchAction(driver);
			PointOption poStart = PointOption.point(startx, y);
			PointOption poEnd = PointOption.point(endx,y);
			act.press(poStart).moveTo(poEnd).release().perform();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 向右滑动
	 * 
	 * @param duration
	 *            滑动时间，单位毫秒
	 */
	public void swipeToRight(int second) {
		int nanos=(int) (second*1000);

		Duration duration = Duration.ofNanos(nanos);
		int startx = appWidth * 1 / 10;
		int endx = appWidth * 9 / 10;
		int y = appHeight * 1 / 2;
		try {
			TouchAction act = new TouchAction(driver);
			PointOption poStart = PointOption.point(startx, y);
			PointOption poEnd = PointOption.point(endx,y);
			act.press(poStart).moveTo(poEnd)
				.waitAction(WaitOptions.waitOptions(duration)).release().perform();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 向上滑动
	 * 
	 * @param duration
	 *            持续时间，单位毫秒
	 */
	public void swipeToUp(int duration) {
		int starty = appHeight * 9 / 10;
		int endy = appHeight * 1 / 10;
		int x = appWidth * 1 / 2;
		try {
			TouchAction act = new TouchAction(driver);
			PointOption poStart = PointOption.point(x, starty);
			PointOption poEnd = PointOption.point(x,endy);
			act.press(poStart).moveTo(poEnd).release().perform();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 向下滑动
	 * 
	 * @param duration
	 *            持续时间，单位毫秒
	 */
	public void swipeToDown(int duration) {
		int starty = appHeight * 1 / 10;
		int endy = appHeight * 9 / 10;
		int x = appWidth * 1 / 2;
		try {
			TouchAction act = new TouchAction(driver);
			PointOption poStart = PointOption.point(x, starty);
			PointOption poEnd = PointOption.point(x,endy);
			act.press(poStart).moveTo(poEnd).release().perform();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 滑动方法，通过参数实现各方向滑动
	 * 
	 * @param direction
	 *            方向参数，值为"up"、"down"、"left"、"right"
	 * @param duration
	 *            滑动时间，单位毫秒
	 */
	public void swipe(String direction, int duration) {
		String direc = direction.toLowerCase();
		if(direc.equals("up")) {
			this.swipeToUp(duration);
		} else if (direc.equals("down")) {
			this.swipeToDown(duration);
		} else if (direc.equals("left")) {
			this.swipeToLeft(duration);
		} else if (direc.equals("right")) {
			this.swipeToRight(duration);
		} else {
			System.out.println("noting to do in the method swipe");
		}
	}
}
